package com.tsunami.auth.service.admin.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.TenantMapper;
import com.tsunami.auth.service.admin.ITenantService;
import com.tsunami.authority.dto.admin.TenantSaveDTO;
import com.tsunami.authority.dto.admin.TenantUpdateDTO;
import com.tsunami.authority.entity.admin.Tenant;
import com.tsunami.authority.enums.admin.TenantType;
import com.tsunami.base.R;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.PageUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Classname TenantServiceImpl
 * @Description TODO
 * @Date 2021-12-02 16:27
 * @Created by lx
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Override
    public R selectPageList(Tenant data) {
        Page<Tenant> pages = baseMapper.selectPageList(PageUtils.buildPage(),data);
        return R.ok().data(pages);
    }

    @Override
    public Tenant saveTenant(SysUser user, TenantSaveDTO data) {
        Tenant tenant = new Tenant();
        tenant.setTenantName(data.getTenantName());
        tenant.setShortName(data.getShortName());
        tenant.setPhone(data.getPhone());
        tenant.setTenantCode(data.getTenantCode());
        tenant.setUserId(data.getUserId());
        if(data.getType().eq(TenantType.T)){
            LocalDateTime now = LocalDateTime.now();
            tenant.setOpenTime(now);
            tenant.setExpireTime(now.plusDays(30));
            tenant.setType(TenantType.T);
        }else {
            LocalDateTime now = LocalDateTime.now();
            tenant.setOpenTime(now);
            tenant.setType(TenantType.F);
            tenant.setExpireTime(null);
        }
        tenant.setCreateUser(user.getId());
        super.save(tenant);
        return tenant;
    }

    @Override
    public boolean remove(List<Long> ids) {
        if(ids!=null&&ids.size()>0){
            for(Long id:ids){
               Tenant tenant = getById(id);
               if(!tenant.getMaster()){
                  removeById(id);
               }else{
                   throw new BizException(ExceptionCode.TENANT_MASTER);
               }
            }
        }
        return false;
    }

    @Override
    public Tenant updateTenant(SysUser user, TenantUpdateDTO data) {
        Tenant tenant = getById(data.getId());
        if(tenant!=null){
            tenant.setTenantName(data.getTenantName());
            tenant.setShortName(data.getShortName());
            tenant.setPhone(data.getPhone());
            tenant.setType(data.getType());
            tenant.setUpdateTime(LocalDateTime.now());
            tenant.setUpdateUser(user.getId());
            super.save(tenant);
            return tenant;
        }
        return null;
    }
}
